package z.myServer;

import java.util.Scanner;

public class MessagePackage {


	public static byte[] serialize(String message) {
		int totalLength = message.length() + 4;
		byte[] buffer = new byte[totalLength];
		int offset = 0;
		
		byte[] totalLengthBuffer = Change.intToByte(totalLength);
		//拷贝4个字节到主byte
		System.arraycopy(totalLengthBuffer, 0, buffer, offset, 4);
		offset += 4;
		
		System.arraycopy(message.getBytes(), 0, buffer, offset, message.length());
		System.out.println("totalLength:" + totalLength);
		return buffer;
		
	}
	
	public static String unserialize(byte[] data) {
		int totalLength = Change.byteToInt(data, 0);
		System.out.println("totalLength:" + totalLength);
		return new String(data, 4, totalLength - 4);
	}
	
//	public static void main(String[] args) {
//		Scanner scanner = new Scanner(System.in);
//		System.out.println("please input...");
//        String p = scanner.nextLine();
//		byte[] bt = MessagePackage.serialize(p);
//		System.out.println(MessagePackage.unserialize(bt));
//	}
}
